package com.longrise.co.ui.login.presenter;

import android.util.Log;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.util.EnCryptDeCrypt;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.login.contract.LoginContract;
import com.longrise.co.ui.login.model.ImageValidateCode;
import com.longrise.co.ui.login.model.LoginBean;
import com.longrise.co.ui.login.model.RegisterBean;
import com.longrise.co.ui.login.model.SmsCodeBean;
import com.ly.common.baserx.RxSubscriber;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.security.EnCryptDeCrypt1;
import com.ly.common.security.EnCryptDeCrypt2;

/**
 * Created by luoyang on 2017-6-23.
 */

public class LoginPresenter extends LoginContract.Presenter {

    private boolean isRegisterSucess = false;

    private boolean isLoginSuccess = false;


    @Override
    public void sendSmsRequest(String mobilePhone, String type) {
        try {
            if (StringUtils.isEmpty(mobilePhone) || StringUtils.isEmpty(type)) {
                mView.showErrorTip("请输入手机号");
                return;
            }

            if(!StringUtils.isChinaPhoneLegal(mobilePhone)) {
                mView.showErrorTip("请输入合法的手机号");
                return;
            }
            EntityBean bean = new EntityBean();
            bean.set("mobile", mobilePhone);
            bean.set("type", type);

            mRxManage.add(mModel.sendSms(bean).subscribe(new RxSubscriber<SmsCodeBean>(mContext, true) {
                @Override
                protected void _onNext(SmsCodeBean detail) {
                    if(mView instanceof LoginContract.RegisterView) {
                        ((LoginContract.RegisterView) mView).sendSmsSuccess(detail);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void loginRequest(String userFlag, String password) {
        try {
            if (StringUtils.isEmpty(userFlag)) {
                mView.showErrorTip("请输入用户名");
                return;
            }
            if (StringUtils.isEmpty(password)) {
                mView.showErrorTip("请输入密码");
                return;
            }
            isLoginSuccess = false;
            EntityBean bean = new EntityBean();
            String enPwd = EnCryptDeCrypt1.getInstance().EnCrypt(password);
            final String enUserFlag = EnCryptDeCrypt2.getInstance().EnCrypt(userFlag);

            bean.set("userflag", userFlag);
            bean.set("password", enPwd);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", enUserFlag);

            mRxManage.add(mModel.login(bean).subscribe(new RxSubscriber<LoginBean>(mContext, true) {
                @Override
                protected void _onNext(LoginBean bean) {
                    try {
                        isLoginSuccess = true;
                        CommonPara.getInstance().setLoginBean(bean);
                        CommonPara.getInstance().setUserAreaid(bean.getAreaid());
                        if(!StringUtils.isEmpty(bean.getUserflag())) {
                            String enCurrentUserFlag = EnCryptDeCrypt2.getInstance().EnCrypt(bean.getUserflag());
                            CommonPara.getInstance().setEnUserFlag(enCurrentUserFlag);
                        }

                        String enAreaId1 = EnCryptDeCrypt2.getInstance().EnCrypt("浙A329QE");
                        String enAreaId2 = EnCryptDeCrypt2.getInstance().EnCrypt("浙A769SY");
                        String enAreaId3 = EnCryptDeCrypt2.getInstance().EnCrypt("浙A0SF12");
                        Log.i("en1", enAreaId1);
                        Log.i("en2", enAreaId2);
                        Log.i("en3", enAreaId3);

                    }catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    if(isLoginSuccess) {
                        mView.requestSuccess(null);
                    }
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void registerRequest(String userFlag, String mobile, String smsCode, String pwd, String rePwd, boolean isCheck, String imageCode, String imageId) {
        try {

            if (StringUtils.isEmpty(userFlag)) {
                mView.showErrorTip("请输入用户名");
                return;
            }

            if (StringUtils.isEmpty(pwd)) {
                mView.showErrorTip("请输入密码");
                return;
            }

            if (StringUtils.isEmpty(rePwd)) {
                mView.showErrorTip("请输入确认密码");
                return;
            }

            if(!pwd.equals(rePwd)) {
                mView.showErrorTip("确认密码与输入密码不一致");
                return;
            }

            if (StringUtils.isEmpty(mobile)) {
                mView.showErrorTip("请输入手机号");
                return;
            }

            if(!StringUtils.isChinaPhoneLegal(mobile)) {
                mView.showErrorTip("请输入合法的手机号");
                return;
            }

            if (StringUtils.isEmpty(smsCode)) {
                mView.showErrorTip("请输入验证码");
                return;
            }

            if(!isCheck) {
                mView.showErrorTip("请同意用户注册协议");
                return;
            }

            if (StringUtils.isEmpty(imageCode)) {
                mView.showErrorTip("请输入图片验证码");
                return;
            }

            EntityBean bean = new EntityBean();
            String enUserFlag = EnCryptDeCrypt2.getInstance().EnCrypt(userFlag);

            bean.set("userflag", userFlag);
            bean.set("mobile", mobile);
            bean.set("smscode", smsCode);
            bean.set("password", pwd);
            bean.set("usertype", "11");
            bean.set("imgcode", imageCode);
            bean.set("imgid", imageId);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", enUserFlag);

            mRxManage.add(mModel.register(bean).subscribe(new RxSubscriber<RegisterBean>(mContext, true) {
                @Override
                protected void _onNext(RegisterBean bean) {
                    isRegisterSucess = true;
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    if(isRegisterSucess) {
                        mView.requestSuccess(null);
                    }
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void resetPwdRequest(String mobile, String password) {
        try {
            isRegisterSucess = false;
            EntityBean bean = new EntityBean();
            String enMobile = EnCryptDeCrypt2.getInstance().EnCrypt(mobile);
            String enPwd = EnCryptDeCrypt.getInstance().EnCrypt(password);

            bean.set("mobile", mobile);
            bean.set("newpassword",enPwd);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", enMobile);

            mRxManage.add(mModel.resetPwd(bean).subscribe(new RxSubscriber<String>(mContext, true) {
                @Override
                protected void _onNext(String bean) {
                    isRegisterSucess = true;
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    if(isRegisterSucess) {
                        mView.requestSuccess(null);
                    }
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void imageCodeRequest() {
        try {
            mRxManage.add(mModel.getImageCode().subscribe(new RxSubscriber<ImageValidateCode>(mContext, true) {
                @Override
                protected void _onNext(ImageValidateCode bean) {
                    if(mView instanceof LoginContract.RegisterView) {
                        ((LoginContract.RegisterView) mView).getImageValidateCodeSuccess(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }
}
